package com.atguigu.serviceedu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.commonutils.ResultCode;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.serviceedu.entity.Teacher;
import com.atguigu.serviceedu.service.TeacherService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.edu.query.TeacherQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-11-15
 */
@Api(description = "讲师相关接口")
@RestController
@RequestMapping("/serviceedu/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @ApiOperation("分布获取讲师")
    @GetMapping("{page}/{size}")
    public R pageList(
            @ApiParam(name="page", value="第几页", required = true) @PathVariable("page") Integer page,
            @ApiParam(name="size", value="每页多少数据", required = true) @PathVariable("size") Integer size) {
        Page<Teacher> p = new Page(page, size);
        IPage<Teacher> pageRes = teacherService.page(p, null);
        List<Teacher> records = pageRes.getRecords();
        long total = pageRes.getTotal();
        long current = pageRes.getCurrent();
        return R.ok().data("list", records).data("total", total).data("current", current);
    }

    @ApiOperation("删除讲师")
    @DeleteMapping("{id}")
    @CrossOrigin
    public R removeById(
            @ApiParam(name="id", value="讲师ID", required = true) @PathVariable("id") Long id) {
        boolean b = teacherService.removeById(id);
        return b ? R.ok() : R.error();
    }

    @ApiOperation("根据条件查询讲师")
    @PostMapping("{page}/{size}")
    public R pageQuery(
            @ApiParam(name="page", value = "第几页", required = true) @PathVariable("page") Integer page,
            @ApiParam(name="size", value="每页几条", required = true) @PathVariable("size") Integer size,
            @ApiParam(name="TeacherQuery", value="查询条件", required = true) @RequestBody TeacherQuery tq) {
        Page<Teacher> p = new Page<>(page, size);
        teacherService.pageQuery(p, tq);

        List<Teacher> records = p.getRecords();
        long total = p.getTotal();

        return R.ok().data("records", records).data("total", total);
    }

    @ApiOperation("新增讲师")
    @PostMapping("save")
    public R save(@ApiParam(name="teacher", value="新增讲师", required = true) @RequestBody Teacher teacher) {
        boolean save = teacherService.save(teacher);
        return save ? R.ok() : R.error();
    }

    @ApiOperation("根据id查询讲师信息")
    @GetMapping("{id}")
    public R getById(@ApiParam(name="id", value="讲师ID", required = true) @PathVariable("id") Long id) {
        Teacher teacher = teacherService.getById(id);
        try {
            int a = 10/0;
        } catch (Exception e) {
            throw new GuliException(20003, "自定义异常");
        }
        return R.ok().data("teacher", teacher);
    }


    @ApiOperation("根据id更新讲师信息")
    @PutMapping()
    public R updateById(@ApiParam(name="teacher", value="讲师信息", required = true) @RequestBody Teacher teacher) {
        boolean b = teacherService.updateById(teacher);
        return b ? R.ok() : R.error();
    }
}

